package sdk.pay.utils.cocos;

import android.app.Activity;
import android.content.Intent;

import sdk.pay.PayUtil;
import sdk.pay.listener.PayGetPayTypeListener;
import sdk.pay.listener.PayUtilCallBack;
import sdk.pay.model.TokenParam;
import sdk.pay.utils.PayLogUtil;

import static sdk.pay.constant.PayExternalConstant.PAY_STATUS;
import static sdk.pay.constant.PayExternalConstant.REQUEST_CODE;
import static sdk.pay.constant.PayExternalConstant.RESULT_CODE;
import static sdk.pay.constant.PayExternalConstant.ZERO_CODE;

/**
 * Created by Joe on 2017/09/27 11:31.
 */

public class PayforCUtil {
    private Activity mActivity = null;
    private PayUtil mPayUtil;
    private PayUtilCallBack mPayUtilCallBack;
    private boolean mPayStatusCallBack;
    private TokenParam mTokenParam = null;

    public native void onCPayException(String arg0);

    public native void onCPayStatus(int payStatus);

    private static final class Handler {
        private static final PayforCUtil mSingle = new PayforCUtil();
    }

    public static PayforCUtil getInstance() {
        return Handler.mSingle;
    }

    public static Object getObjectInstance() {
        return Handler.mSingle;
    }

    public void initPay(Activity activity, boolean payStatusCallBack) {
        mActivity = activity;
        if (mActivity == null) {
            PayLogUtil.logBase("PayforCUtil activity null");
            return;
        }
        mPayUtilCallBack = new PayUtilCallBack() {
            @Override
            public void onPayException(String exceptionMsg) {
                try {
                    onCPayException(exceptionMsg);
                } catch (Exception e) {
                    e.printStackTrace();
                    PayLogUtil.logBase("not find onCPayException c method");
                }
            }
        };
        mPayStatusCallBack = payStatusCallBack;
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPayUtil = new PayUtil(mActivity, mPayUtilCallBack, mPayStatusCallBack);
            }
        });
    }

    public void setPayParam(final String... payParam) {
        if (mActivity == null) {
            PayLogUtil.logBase("PayforCUtil not init");
            return;
        }
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mPayUtil != null && payParam != null && payParam.length > 0) {
                    mPayUtil.setPayParam(payParam);
                }
            }
        });
    }

    public PayUtil getPayUtil() {
        return mPayUtil;
    }

    public void pay(int type) {
        if (mTokenParam == null) {
            return;
        }
        mPayUtil.pay(mTokenParam, type);
    }

    public void setTokenParam(TokenParam tokenParam) {
        mTokenParam = tokenParam;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CODE == requestCode) {
            if (RESULT_CODE == resultCode) {
                int payStatus = data.getIntExtra(PAY_STATUS, ZERO_CODE);
                try {
                    onCPayStatus(payStatus);
                } catch (Exception e) {
                    e.printStackTrace();
                    PayLogUtil.logBase("not find onCPayStatus c method");
                }
            }
        }
    }
}
